/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.component.panel;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import towerz.game.Sprite;
import towerz.game.entity.hut.HutData;
import towerz.game.entity.hut.HutFactory;
import towerz.game.entity.tower.TowerFactory;
import towerz.game.level.Level;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.StringManager;
import towerz.system.Utilities;

/**
 *
 * @author LegACy
 */
public class HutCreate extends Panel {
	public HutCreate(Image image, int x, int y) {
		//Super
		super(image, x, y);
		
		//Load arrow image
		try {
			m_Arrow = new Sprite(Utilities.PANELARROW_IMAGE, 0, 0, 3, 1);
			m_Arrow.setPosition(m_X + ((m_Background.getWidth() - m_Arrow.getWidth()) / 2), m_Y + 24);
		} catch (IOException ex) {}
	}

	public void show(Level level) {
		//Super
		super.show();

		//Set data
		m_Current	= 0;
		m_Level		= level;

		//Get available huts
		Vector Huts		= new Vector();
		int[] Available = m_Level.getAvailableHuts();
		int[] Basics	= HutFactory.instance().getBasicHuts();

		//Find basic available huts
		for (int i = 0; i < Available.length; i++) {
			boolean Exist = false;

			for (int j = 0; j < Basics.length; j++)
				if (Available[i] == Basics[j]) Exist = true;

			//If exist, add to vector
			if (Exist) Huts.addElement(new Integer(Available[i]));
		}

		
		//Copy to array
		m_Huts = new int[Huts.size()];
		for (int i = 0; i < m_Huts.length; i++) m_Huts[i] = ((Integer)Huts.elementAt(i)).intValue();

		//Save image BitmapFont
		m_StringImg = new Image[4];

		//Get data
		HutData Data = HutFactory.instance().getHutData(m_Huts[m_Current]);

		//Create data image
		String Cost		= StringManager.instance().getString("cost") + " " + Data.getCost() + " " + StringManager.instance().getString("culture");
		m_StringImg[0]	= Data.getNameString();
		m_StringImg[1]	= Data.getDescString();
		m_StringImg[2]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(Cost , 0x00000000);
		m_StringImg[3]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("summon") + ":", 0x00000000);
	}

	public HutData getSelected() {
		//Default
		HutData Selected = null;

		//Check gold
		if (m_Gold >= HutFactory.instance().getHutData(m_Huts[m_Current]).getCost())
				Selected = HutFactory.instance().getHutData(m_Huts[m_Current]);
		
		//Return tower data
		return Selected;
	}

	public void hide() {
		//Super
		super.hide();

		m_StringImg = null;

		//Reset
		m_Current = 0;
	}

	public void update(int keys, int gold, long time) {
		//Super
		super.update(keys, gold, time);

		//Reset arrow
		if (m_Time <= 0) m_Arrow.setFrame(0);

		//Check keys
		if ((keys & GameCanvas.LEFT_PRESSED) != 0)	{ //awalnya UP
			//Previous
            m_Current--;

			//Check selection
			if (m_Current >= m_Huts.length) m_Current = 0;
			else if (m_Current < 0)			m_Current = m_Huts.length - 1;

			//Get data
			HutData Data = HutFactory.instance().getHutData(m_Huts[m_Current]);

			//Create data image
			String Cost		= StringManager.instance().getString("cost") + " " + Data.getCost() + " " + StringManager.instance().getString("culture");
			m_StringImg[2]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(Cost , 0x00000000);
			m_StringImg[1]	= Data.getDescString();
			m_StringImg[0]	= Data.getNameString();

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.setFrame(1);
        }

		if ((keys & GameCanvas.RIGHT_PRESSED) != 0)	{ //awalnya DOWN
			//
            m_Current++;

			//Check selection
			if (m_Current >= m_Huts.length) m_Current = 0;
			else if (m_Current < 0)			m_Current = m_Huts.length - 1;

			//Get data
			HutData Data = HutFactory.instance().getHutData(m_Huts[m_Current]);

			//Create data image
			String Cost		= StringManager.instance().getString("cost") + " " + Data.getCost() + " " + StringManager.instance().getString("culture");
			m_StringImg[2]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(Cost , 0x00000000);
			m_StringImg[1]	= Data.getDescString();
			m_StringImg[0]	= Data.getNameString();

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.setFrame(2);
        }
		
		if ((keys & GameCanvas.LEFT_PRESSED) != 0 || (keys & GameCanvas.RIGHT_PRESSED) != 0) {
			if (m_Huts.length > 1) {
				//Cursor sound
				StateManager.instance().playSFX("/cursor.wav");
			}
		}
	}

	public void draw(Graphics g) {
		//Skip if no image or data
		if (m_Huts == null || m_StringImg == null) return;
		super.draw(g);

		//Draw arrow
		if (m_Huts.length > 1) m_Arrow.draw(g);

		//Get data
		HutData Data = HutFactory.instance().getHutData(m_Huts[m_Current]);

		//Draw hut
		g.drawImage(Data.getImage(), m_X + (m_Background.getWidth() / 2), m_Y + 8 , Graphics.HCENTER| Graphics.TOP);

		//Draw data
		if (m_StringImg[0] != null) g.drawImage(m_StringImg[0], m_X + (m_Background.getWidth() / 2), m_Y + Data.getImage().getHeight() + 12,  Graphics.HCENTER| Graphics.TOP);
		if (m_StringImg[1] != null) g.drawImage(m_StringImg[1],	m_X + 8, m_Y + Data.getImage().getHeight() + 26,  Graphics.LEFT | Graphics.TOP);
		if (m_StringImg[2] != null) g.drawImage(m_StringImg[2], m_X + 8, m_Y + Data.getImage().getHeight() + 48,  Graphics.LEFT| Graphics.TOP);
		if (m_StringImg[3] != null) g.drawImage(m_StringImg[3], m_X + 8, m_Y + Data.getImage().getHeight() + 64,  Graphics.LEFT| Graphics.TOP);

		//Draw available towers
		int[] Towers = Data.getTowers(m_Level);
		for (int i = 0; i < Towers.length; i++) {
			//Draw image
			Image TowerImage = TowerFactory.instance().getTowerData(Towers[i]).getIcon();
			g.drawImage(TowerImage, m_X + (m_Background.getWidth() / 2) + ((i - 1) * TowerImage.getWidth()), m_Y + m_Background.getHeight() - 12, Graphics.HCENTER | Graphics.BOTTOM);
		}
	}

	//Members
	protected int[]		m_Huts;
	protected int		m_Current;
	protected Level		m_Level;
	protected Sprite	m_Arrow;
	protected Image[]	m_StringImg;
}
